import { createRouter, createWebHistory } from 'vue-router';

// 导入主页推送模块
import homePagePostRoutes from '../router/routes/homePagePost';
// 登录注册模块
import loginRoutes from '../router/routes/loginRoute';
// 创作中心模块
import createRoutes from '../router/routes/createCenter';
// 消息模块
import messageRoutes from '../router/routes/message';
// 导入管理员路由配置
import adminRoutes from '../router/routes/admin';
// 导入个人中心模块路由配置
import personalRoutes from '../router/routes/personal';

const routes = [
    // 添加重定向规则
    {
        path: '/',
        redirect: '/login' // 自动跳转到登录页
    },
    ...loginRoutes,
    ...homePagePostRoutes,
    ...createRoutes,
    // 添加内容详情页路由
    {
        path: '/content',
        name: 'ContentView',
        component: () => import('@/views/create/Content.vue')
    },
    ...messageRoutes,
    ...adminRoutes,
    {
        path: '/search',
        name: 'SearchView',
        // 添加搜索页面的懒加载路由
        component: () => import(/* webpackChunkName: "search" */ '../views/search/SearchView.vue'),
        // 将查询参数 q 作为 prop 传递给组件
        props: (route) => ({ query: route.query.q })
    },
    // 添加个人中心模块路由
    ...personalRoutes
];

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    }
});

export default router;